<?php 
// Author : evilair
// Time   : 2017/07/25

/************************************************
* 
* 给定一个升序排列的数组,找出两个数和为一个给定的数
* Input: numbers = [2, 7, 11, 15] target = 9
* output: index1 = 1, index2 = 2
*/

$numbers = [2, 7, 11, 15];
$target = 9;
function solution($numbers, $target) {
  $low = 0;
  $high = count($numbers) - 1;
  $result = [];
  while ($low < $high) {
    if ($numbers[$low] + $numbers[$high] == $target) {
      $result['index1'] = $low + 1;
      $result['index2'] = $high + 1;
      break;
    } else {
      $numbers[$low] + $numbers[$high] > $target ? $high-- : $low++;
    }
  }
  return $result;
}

$output = solution($numbers, $target);
var_dump($output);
?>